package org.wzy.xmlg_backend.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wzy.xmlg_backend.dto.AnnouncementDTO;
import org.wzy.xmlg_backend.entity.Announcement;
import org.wzy.xmlg_backend.entity.Result;
import org.wzy.xmlg_backend.service.IAnnouncementService;

import java.util.List;

@RestController
@RequestMapping("/api/announcement")
@Tag(name = "公告管理", description = "公告管理相关接口")
@CrossOrigin
public class AnnouncementController {
    
    @Autowired
    private IAnnouncementService announcementService;
    
    @PostMapping("/create")
    @Operation(summary = "创建公告（发布或草稿）")
    public Result<String> createAnnouncement(@RequestBody AnnouncementDTO announcementDTO) {
        if (announcementDTO.getTitle() == null || announcementDTO.getTitle().trim().isEmpty()) {
            return Result.fail("公告标题不能为空");
        }
        if (announcementDTO.getContent() == null || announcementDTO.getContent().trim().isEmpty()) {
            return Result.fail("公告内容不能为空");
        }
        if (announcementDTO.getPublisherId() == null) {
            return Result.fail("发布人ID不能为空");
        }
        
        Announcement announcement = new Announcement();
        announcement.setTitle(announcementDTO.getTitle());
        announcement.setContent(announcementDTO.getContent());
        announcement.setStatus(announcementDTO.getStatus() != null ? announcementDTO.getStatus() : 0);
        announcement.setPublisherId(announcementDTO.getPublisherId());
        
        boolean success;
        if (announcement.getStatus() == 1) {
            success = announcementService.publishAnnouncement(announcement, announcement.getPublisherId());
            return success ? Result.success("公告发布成功") : Result.fail("公告发布失败");
        } else {
            success = announcementService.saveDraft(announcement, announcement.getPublisherId());
            return success ? Result.success("草稿保存成功") : Result.fail("草稿保存失败");
        }
    }
    
    @GetMapping("/list")
    @Operation(summary = "获取公告列表")
    public Result<List<AnnouncementDTO>> getAnnouncementList() {
        List<AnnouncementDTO> announcements = announcementService.getAnnouncementDTOList();
        return Result.success(announcements);
    }
    
    @GetMapping("/list/{publisherId}")
    @Operation(summary = "根据发布人获取公告列表")
    public Result<List<AnnouncementDTO>> getAnnouncementsByPublisher(@PathVariable Integer publisherId) {
        List<AnnouncementDTO> announcements = announcementService.getAnnouncementDTOsByPublisher(publisherId);
        return Result.success(announcements);
    }
    
    @PutMapping("/update")
    @Operation(summary = "更新公告")
    public Result<String> updateAnnouncement(@RequestBody AnnouncementDTO announcementDTO) {
        if (announcementDTO.getId() == null) {
            return Result.fail("公告ID不能为空");
        }
        if (announcementDTO.getTitle() == null || announcementDTO.getTitle().trim().isEmpty()) {
            return Result.fail("公告标题不能为空");
        }
        if (announcementDTO.getContent() == null || announcementDTO.getContent().trim().isEmpty()) {
            return Result.fail("公告内容不能为空");
        }
        
        Announcement announcement = new Announcement();
        announcement.setId(announcementDTO.getId());
        announcement.setTitle(announcementDTO.getTitle());
        announcement.setContent(announcementDTO.getContent());
        announcement.setStatus(announcementDTO.getStatus());
        
        boolean success = announcementService.updateAnnouncement(announcement);
        return success ? Result.success("公告更新成功") : Result.fail("公告更新失败");
    }
    
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除公告")
    public Result<String> deleteAnnouncement(@PathVariable Integer id) {
        boolean success = announcementService.deleteAnnouncement(id);
        return success ? Result.success("公告删除成功") : Result.fail("公告删除失败");
    }
    
    @GetMapping("/detail/{id}")
    @Operation(summary = "获取公告详情")
    public Result<AnnouncementDTO> getAnnouncementDetail(@PathVariable Integer id) {
        AnnouncementDTO announcement = announcementService.getAnnouncementDTOById(id);
        return announcement != null ? Result.success(announcement) : Result.fail("公告不存在");
    }
}